package com.ssi.myshop.Presenter.impl;

import com.ssi.myshop.Presenter.IBannerPresenter;
import com.ssi.myshop.base.BaseListData;
import com.ssi.myshop.base.BaseListObserver;
import com.ssi.myshop.bean.BannerBean;
import com.ssi.myshop.contants.Api;
import com.ssi.myshop.utils.RetrofitManager;
import com.ssi.myshop.utils.RxUtil;
import com.ssi.myshop.view.IBannerCallBack;

import java.util.List;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class IBannerPresenterImpl implements IBannerPresenter {
    private static final String TAG = "IBannerPresenterImpl";
    private IBannerCallBack mCallBack;
    private Retrofit mRetrofit;
    private Api mApi;

    @Override
    public void getBannerList() {
        mRetrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = mRetrofit.create(Api.class);
        Observable<BaseListData<BannerBean>> observable = mApi.getBanner();
        observable.compose(RxUtil.schedulerTransformer())
                .subscribeWith(new BaseListObserver<BannerBean>(mCallBack) {

                    @Override
                    protected void onSuccess(List<BannerBean> data) {
                        mCallBack.getBannerSuccess(data);
                    }
                });

//        Call<BannerBean> task = mApi.getBanner();
//        task.enqueue(new Callback<BannerBean>() {
//            @Override
//            public void onResponse(Call<BannerBean> call, Response<BannerBean> response) {
//                Log.d(TAG, response.body().toString());
//                mCallBack.getBannerSuccess();
//            }
//
//            @Override
//            public void onFailure(Call<BannerBean> call, Throwable t) {
//                Log.d(TAG, "onFailure: "+t.toString());
//            }
//        });

//        OkHttpClient okHttpClient = new OkHttpClient();
//        Request request = new Request.Builder()
//                .url("https://api.it120.cc/tristan/banner/list")
//                .get()
//                .build();
//        Call call = okHttpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.d(TAG, "onFailure: " + e.toString());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//
////                Log.d(TAG, "onResponse: " + response.body().string());
//                Gson gson = new Gson();
//                BannerBean bannerBean = gson.fromJson(response.body().string(), BannerBean.class);
//                Log.d(TAG, "onResponse: "+bannerBean.getData());
//            }
//        });
//        Toast.makeText(BaseApplication.getAppContext(), "获取数据", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void registerViewCallBack(IBannerCallBack callBack) {
        mCallBack = callBack;
    }

    @Override
    public void unregisterViewCallBack(IBannerCallBack callBack) {
        if (mCallBack != null) {
            mCallBack = null;
        }
    }
}
